<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class tree_refresh extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'songshu:tree_refresh';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新数的团队总数队列';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        while(true){
            $tree_id =  Redis::lpop('queue:tree');
            if($tree_id!=null){
                DB::table('user_tree')->where('tree_id',$tree_id)->update([
                    'all_count'=>DB::raw("(`right`-`left`-1)/2")
                ]);
                DB::disconnect('mysql');
            }
            sleep(2);
        }
    }
}
